/**
 * 
 */
package tani.bnn.com.mbilling.main;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.jxls.exception.ParsePropertyException;
import net.sf.jxls.transformer.XLSTransformer;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import tani.bnn.com.mbilling.core.Product;
import tani.bnn.com.mbilling.core.SalePurchase;
import tani.bnn.com.mbilling.core.SalePurchaseDetails;
import tani.bnn.com.mbilling.dao.SalePurchaseDAO;
import tani.bnn.com.mbilling.utility.DateUtils;
import tani.bnn.com.mbilling.utility.MBillingConstant;
import tani.bnn.com.mbilling.utility.Utility;

/**
 * @author john
 *
 */
public class GenerateStockReport {
	public static String templateFileName = "templates/BalanceStock.xls";

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		List<SalePurchase> stockList = null;
		SalePurchaseDetails stock = new SalePurchaseDetails(null);
		String destFileName = "";
		String headerText = null;
		try {
			switch(args.length){
				case 0:
					stockList = new SalePurchaseDAO().getProductStockList(null);
					headerText = "All available products";
					break;
				default:
					stockList = new SalePurchaseDAO().getProductStockList(Integer.valueOf(args[0]));
					headerText = Integer.valueOf(args[0]) == 0 ? "All Products not in stock":
						Integer.valueOf(args[0]) == -1 ? "All Products sold without Purchase Entered":
							"All product having non zero Stock but upto " + args[0];
			}
			stock.setReportHeader("Current Stock availability for " + headerText);

			stock.setSalePurchases(stockList);
			destFileName = Utility.getAppUtility().getProperty(MBillingConstant.OUTPUT) 
					+ File.separator + "Stock_Report_" + DateUtils.now(DateUtils.DATE_FILE_FORMAT) + ".xls";
			XLSTransformer transformer = new XLSTransformer();
			Map<String, SalePurchaseDetails> beans = new HashMap<String, SalePurchaseDetails>();
			beans.put("stock", stock);
			//transformer.setColumnPropertyNamesToHide(new String[]{"age"});
			//transformer.setColumnsToHide(new short[]{1});
			transformer.groupCollection("products0.all.name");
			transformer.transformXLS(GenerateStockReport.templateFileName, beans, destFileName);
		} catch (ParsePropertyException e) {
			e.printStackTrace();
		} catch (InvalidFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
